package ch.gibb.learnit.client.util;

import ch.gibb.learnit.client.util.soap.*;

import java.util.List;

/**
 * This class is here to have an easier access to the service. All needed methods are implemented here
 *
 * @author Bernard Jaquet
 * @version 1.0
 */
public class ServiceAccess {
    private static ILearnIT client;
    private ObjectFactory factory = new ObjectFactory();

    private ILearnIT getClient() {
        if(client == null){
            LearnIT service = new LearnIT();
            client = service.getBasicHttpBindingILearnIT();
        }
        return client;
    }

    public User getUser(int id) {
        User tmp = factory.createUser();
        tmp.setId(id);
        return getClient().getUser(tmp);
    }

    public int addUser(User user) {
        return getClient().addUser(user);
    }

    public boolean updateUser(User user) {
        return getClient().updateUser(user);
    }

    public int login(User user) {
        return getClient().login(user);
    }

    public boolean resetPassword(User user, String password) {
        return getClient().resetPassword(user, password);
    }

    public boolean addCategory(User user, Category cat) {
        return getClient().addCategory(user, cat);
    }

    public List<Category> getCategories(User user) {
        return getClient().getCategories(user).getCategory();
    }

    public boolean removeCategories(User user, List<Category> cats){
        ArrayOfCategory arr = factory.createArrayOfCategory();
        arr.setCategory(cats);
        return getClient().removeCategories(user, arr);
    }

    public int getDayliWordsCount(User user) {
        return getClient().getDailyWordsCount(user);
    }

    public boolean addWord(User user, Category cat, Word word) {
        return getClient().addWord(user, cat, word);
    }

    public Word getWord(User user, int id) {
        Word w = factory.createWord();
        w.setId(id);
        return getClient().getWordById(user, w);
    }

    public Word getWord(User user) {
        return getClient().getWord(user);
    }

    public Word getWord(User user, Category cat) {
        return getClient().getWordOfCat(user, cat);
    }

    public List<Word> getAllWords(User user) {
        return getClient().getAllWords(user).getWord();
    }

    public List<Word> getAllWords(User user, Category cat) {
        return getClient().getAllWordsOfCat(user, cat).getWord();
    }

    public boolean updateWord (User user, Category cat, Word word) {
        return getClient().updateWord(user, word);
    }

    public boolean removeWords (User user, List<Word> words) {
        ArrayOfWord arr = factory.createArrayOfWord();
        arr.setWord(words);
        return getClient().removeWords(user, arr);
    }

    public Category getCategory(User user, int category) {
        Category cat = factory.createCategory();
        cat.setId(category);
        return client.getCategoryById(user, cat);
    }

    public boolean respondWord(User user, Word word, boolean responce) {
        if (responce)
            return getClient().responseWord(user, word);
        return true;
    }
}
